
<!DOCTYPE HTML>
<html lang="en" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>2.4 会话AioSession · smartboot 中文指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../SUMMARY.html" />
    
    
    <link rel="prev" href="../客户端/" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../">
            
                <a href="../../../">
            
                    
                    关于smartboot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    smart-socket 使用手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" >
            
                <span>
            
                    
                    第一章 smart-socket开发指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.2.1" data-path="../../chapter-1/2.1-基础应用/">
            
                <a href="../../chapter-1/2.1-基础应用/">
            
                    
                    1.1 快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.2" data-path="../../chapter-1/2.2-Spring集成/">
            
                <a href="../../chapter-1/2.2-Spring集成/">
            
                    
                    1.2 Spring集成smart-socket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.3" data-path="../../chapter-1/SUMMARY.html">
            
                <a href="../../chapter-1/SUMMARY.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../">
            
                <a href="../">
            
                    
                    第二章 源码解析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.3.1" data-path="../核心接口/">
            
                <a href="../核心接口/">
            
                    
                    2.1 核心接口与状态机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.2" data-path="../服务端/">
            
                <a href="../服务端/">
            
                    
                    2.2 服务端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.3" data-path="../客户端/">
            
                <a href="../客户端/">
            
                    
                    2.3 客户端
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.3.4" data-path="./">
            
                <a href="./">
            
                    
                    2.4 会话AioSession
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.5" data-path="../SUMMARY.html">
            
                <a href="../SUMMARY.html">
            
                    
                    2.5 小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../chapter-3/">
            
                <a href="../../chapter-3/">
            
                    
                    第三章 通信协议
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.4.1" data-path="../../chapter-3/1-消息归类/">
            
                <a href="../../chapter-3/1-消息归类/">
            
                    
                    3.1 消息归类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.2" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.2 半包/粘包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.3" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.3 Http消息解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.4" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.4 RPC消息解析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../../chapter-5/">
            
                <a href="../../chapter-5/">
            
                    
                    第四章 内存池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../../chapter-6/">
            
                <a href="../../chapter-6/">
            
                    
                    第五章 未来规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../../end/">
            
                <a href="../../end/">
            
                    
                    附录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    smart-http 实战手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../../smart-http/">
            
                <a href="../../../smart-http/">
            
                    
                    关于smart-http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" >
            
                <span>
            
                    
                    第一章 smart-http开发指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.2.1" data-path="../../../smart-http/chapter-1/">
            
                <a href="../../../smart-http/chapter-1/">
            
                    
                    1.1 快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.2" data-path="../../../smart-http/chapter-1/http_route.html">
            
                <a href="../../../smart-http/chapter-1/http_route.html">
            
                    
                    1.2 请求路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.3" data-path="../../../smart-http/chapter-1/file_upload.html">
            
                <a href="../../../smart-http/chapter-1/file_upload.html">
            
                    
                    1.3 文件上传/下载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.4" data-path="../../../smart-http/chapter-1/websocket.html">
            
                <a href="../../../smart-http/chapter-1/websocket.html">
            
                    
                    1.4 WebSocket
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    技术分享
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../../share/rpc/smart-socket-rpc.html">
            
                <a href="../../../share/rpc/smart-socket-rpc.html">
            
                    
                    smart-socket实现RPC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../../share/firstfit/readme.html">
            
                <a href="../../../share/firstfit/readme.html">
            
                    
                    内存管理-首次适应算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../../share/thread/readme.html">
            
                <a href="../../../share/thread/readme.html">
            
                    
                    Thread知多少
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../../donation.html">
            
                <a href="../../../donation.html">
            
                    
                    关于捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../.." >2.4 会话AioSession</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x901A;&#x4FE1;&#x4F1A;&#x8BDD;aiosession">&#x901A;&#x4FE1;&#x4F1A;&#x8BDD;AioSession</h3>
<p>&#x200B;    AioQuickServer&#x548C;AioQuickClient&#x5728;smart-socket&#x4E2D;&#x8D1F;&#x8D23;&#x7684;&#x662F;&#x670D;&#x52A1;&#x7684;&#x914D;&#x7F6E;&#x3001;&#x542F;&#x52A8;&#x3001;&#x505C;&#x6B62;&#xFF0C;&#x6240;&#x4EE5;&#x4EE3;&#x7801;&#x903B;&#x8F91;&#x8F83;&#x7B80;&#x5355;&#x3002;AioSession&#x624D;&#x662F;smart-socket&#x771F;&#x6B63;&#x7684;&#x7075;&#x9B42;&#xFF0C;&#x5B83;&#x662F;&#x8854;&#x63A5;&#x7F51;&#x7EDC;&#x4F20;&#x8F93;&#x4E0E;&#x4E1A;&#x52A1;&#x5E94;&#x7528;&#x7684;&#x7EBD;&#x5E26;&#x3002;&#x5728;AioSession&#x7684;&#x534F;&#x8C03;&#x63A7;&#x5236;&#x4E0B;&#xFF0C;&#x7528;&#x6237;&#x65E0;&#x9700;&#x518D;&#x53BB;&#x5173;&#x5FC3;&#x5E76;&#x53D1;&#x6240;&#x5E26;&#x6765;&#x7684;&#x590D;&#x6742;IO&#x573A;&#x666F;&#xFF0C;&#x53EA;&#x9700;&#x4E13;&#x6CE8;&#x4E8E;&#x6570;&#x636E;&#x7F16;&#x89E3;&#x7801;&#x4E0E;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#x3002;</p>
<p>&#x200B;    AioSession&#x6211;&#x4EEC;&#x53EA;&#x8BB2;&#x89E3;&#x8DDF;&#x7528;&#x6237;&#x606F;&#x606F;&#x76F8;&#x5173;&#x7684;&#x90E8;&#x5206;&#xFF0C;&#x5176;&#x4F59;&#x90E8;&#x5206;&#x8BFB;&#x8005;&#x53EF;&#x81EA;&#x884C;&#x53BB;&#x9605;&#x8BFB;&#x6E90;&#x7801;&#x3002;AIO&#x901A;&#x4FE1;&#x7684;&#x5173;&#x952E;&#x4E24;&#x4E2A;&#x73AF;&#x8282;&#x4E3A;&#xFF1A;&#x8BFB;&#x56DE;&#x8C03;&#x3001;&#x5199;&#x56DE;&#x8C03;&#xFF0C;smart-socket&#x5BF9;&#x8FD9;&#x4E24;&#x4E2A;&#x4E8B;&#x4EF6;&#x7684;&#x5904;&#x7406;&#x90FD;&#x5728;AioSession&#x4E2D;&#x5B9E;&#x73B0;&#x3002;&#x968F;&#x7740;&#x7248;&#x672C;&#x7684;&#x8FED;&#x4EE3;&#xFF0C;&#x8BFB;&#x8005;&#x5728;&#x672C;&#x6587;&#x770B;&#x5230;&#x7684;&#x4EE3;&#x7801;&#x53EF;&#x80FD;&#x4E0E;&#x5B9E;&#x9645;&#x9879;&#x76EE;&#x6E90;&#x7801;&#x4E2D;&#x770B;&#x5230;&#x7684;&#x53EF;&#x80FD;&#x7565;&#x6709;&#x5DEE;&#x5F02;&#xFF0C;&#x4F46;&#x4E3B;&#x4F53;&#x7ED3;&#x6784;&#x662F;&#x4E00;&#x81F4;&#x7684;&#x3002;&#x6211;&#x4EEC;&#x5148;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x8BFB;&#x56DE;&#x8C03;&#x7684;&#x5904;&#x7406;&#x8FC7;&#x7A0B;&#xFF1A;</p>
<ol>
<li>&#x8C03;&#x7528;flip&#x65B9;&#x6CD5;&#x5C06;&#x63A5;&#x6536;&#x6570;&#x636E;&#x7684;&#x7F13;&#x51B2;&#x533A;&#x5207;&#x6362;&#x81F3;&#x8BFB;&#x6A21;&#x5F0F;&#x3002;</li>
<li>&#x6267;&#x884C;<code>ioServerConfig.getProtocol().decode()</code>&#x8FDB;&#x884C;&#x6570;&#x636E;&#x89E3;&#x6790;&#x3002;&#x82E5;&#x65E0;&#x6CD5;&#x89E3;&#x6790;&#x51FA;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x7684;&#x6D88;&#x606F;&#x5219;&#x8FD4;&#x56DE;null&#xFF0C;&#x7ED3;&#x675F;&#x672C;&#x6B21;&#x89E3;&#x6790;&#x64CD;&#x4F5C;&#xFF1B;&#x82E5;&#x5728;&#x89E3;&#x6790;&#x8FC7;&#x7A0B;&#x4E2D;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x4F1A;&#x89E6;&#x53D1;&#x72B6;&#x6001;&#x673A;<strong>PROCESS_EXCEPTION</strong>&#xFF0C;&#x5728;&#x8FD9;&#x4E2A;&#x73AF;&#x8282;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x901A;&#x5E38;&#x662F;&#x56E0;&#x4E3A;&#x63A5;&#x6536;&#x5230;&#x4E86;&#x975E;&#x6CD5;&#x6570;&#x636E;&#xFF0C;&#x6B64;&#x7C7B;&#x95EE;&#x9898;&#x7684;&#x5E38;&#x89C1;&#x5904;&#x7406;&#x65B9;&#x5F0F;&#x4E3A;&#x5173;&#x95ED;&#x5F53;&#x524D;&#x7F51;&#x7EDC;&#x3002;</li>
<li>&#x5982;&#x679C;&#x6210;&#x529F;&#x89E3;&#x6790;&#x51FA;&#x4E00;&#x4E2A;&#x4E1A;&#x52A1;&#x6D88;&#x606F;&#xFF0C;&#x4F1A;&#x6267;&#x884C;&#x6D88;&#x606F;&#x5904;&#x7406;&#x5668;<code>messageProcessor.process(this, dataEntry)</code>&#x3002;&#x8FD9;&#x4E2A;&#x9636;&#x6BB5;&#x51FA;&#x73B0;&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x5F02;&#x5E38;&#x5E76;&#x4E0D;&#x5F71;&#x54CD;&#x539F;&#x7F51;&#x7EDC;&#x4F20;&#x8F93;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x6709;&#x6548;&#x6027;&#xFF0C;&#x6240;&#x4EE5;smart-socket&#x5BF9;&#x5F02;&#x5E38;&#x8FDB;&#x884C;&#x4E86;&#x6355;&#x83B7;&#x5E76;&#x4F9D;&#x65E7;&#x4EE5;&#x72B6;&#x6001;&#x673A;<strong>PROCESS_EXCEPTION</strong>&#x7684;&#x5F62;&#x5F0F;&#x53CD;&#x9988;&#x7ED9;&#x7528;&#x6237;&#x3002;</li>
<li>&#x5B8C;&#x6210;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#x540E;&#x82E5;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x8FD8;&#x5B58;&#x7559;&#x672A;&#x89E3;&#x6790;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4F1A;&#x518D;&#x6B21;&#x6267;&#x884C;&#x89E3;&#x7801;&#xFF08;&#x6B65;&#x9AA4;2&#xFF09;&#x3001;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#xFF08;&#x6B65;&#x9AA4;3&#xFF09;&#x64CD;&#x4F5C;&#x3002;</li>
<li>byteBuf&#x4E2D;&#x5B58;&#x653E;&#x4E86;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#x540E;&#x5F85;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x6267;&#x884C;flush&#x8FDB;&#x884C;&#x6570;&#x636E;&#x8F93;&#x51FA;&#x3002;</li>
<li>&#x76D1;&#x6D4B;&#x5F53;&#x524D;AioSession&#x72B6;&#x6001;&#xFF0C;&#x5982;&#x82E5;&#x5DF2;&#x4E0D;&#x53EF;&#x7528;&#x5219;&#x7ED3;&#x675F;&#x3002;</li>
<li>&#x82E5;AioSession&#x4F9D;&#x65E7;&#x5904;&#x4E8E;&#x53EF;&#x7528;&#x72B6;&#x6001;&#xFF0C;&#x5219;&#x5207;&#x6362;&#x7F13;&#x51B2;&#x533A;&#x81F3;&#x5199;&#x72B6;&#x6001;&#xFF0C;&#x5E76;&#x89E6;&#x53D1;&#x8BFB;&#x64CD;&#x4F5C;<code>continueRead</code>&#x3002;</li>
</ol>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">readFromChannel</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> eof)</span> </span>{
    <span class="hljs-keyword">final</span> ByteBuffer readBuffer = <span class="hljs-keyword">this</span>.readBuffer.buffer();
    readBuffer.flip();
    <span class="hljs-keyword">final</span> MessageProcessor&lt;T&gt; messageProcessor = ioServerConfig.getProcessor();
    <span class="hljs-keyword">while</span> (readBuffer.hasRemaining()) {
        T dataEntry = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">try</span> {
            dataEntry = ioServerConfig.getProtocol().decode(readBuffer, <span class="hljs-keyword">this</span>);
        } <span class="hljs-keyword">catch</span> (Exception e) {
            messageProcessor.stateEvent(<span class="hljs-keyword">this</span>, StateMachineEnum.DECODE_EXCEPTION, e);
            <span class="hljs-keyword">throw</span> e;
        }
        <span class="hljs-keyword">if</span> (dataEntry == <span class="hljs-keyword">null</span>) {
            <span class="hljs-keyword">break</span>;
        }

        <span class="hljs-comment">//&#x5904;&#x7406;&#x6D88;&#x606F;</span>
        <span class="hljs-keyword">try</span> {
            messageProcessor.process(<span class="hljs-keyword">this</span>, dataEntry);
        } <span class="hljs-keyword">catch</span> (Exception e) {
            messageProcessor.stateEvent(<span class="hljs-keyword">this</span>, StateMachineEnum.PROCESS_EXCEPTION, e);
        }
    }
    <span class="hljs-keyword">if</span> (byteBuf != <span class="hljs-keyword">null</span> &amp;&amp; !byteBuf.isClosed()) {
        byteBuf.flush();
    }

    <span class="hljs-keyword">if</span> (eof || status == SESSION_STATUS_CLOSING) {
        close(<span class="hljs-keyword">false</span>);
        messageProcessor.stateEvent(<span class="hljs-keyword">this</span>, StateMachineEnum.INPUT_SHUTDOWN, <span class="hljs-keyword">null</span>);
        <span class="hljs-keyword">return</span>;
    }
    <span class="hljs-keyword">if</span> (status == SESSION_STATUS_CLOSED) {
        <span class="hljs-keyword">return</span>;
    }

    <span class="hljs-comment">//&#x6570;&#x636E;&#x8BFB;&#x53D6;&#x5B8C;&#x6BD5;</span>
    <span class="hljs-keyword">if</span> (readBuffer.remaining() == <span class="hljs-number">0</span>) {
        readBuffer.clear();
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (readBuffer.position() &gt; <span class="hljs-number">0</span>) {
        <span class="hljs-comment">// &#x4EC5;&#x5F53;&#x53D1;&#x751F;&#x6570;&#x636E;&#x8BFB;&#x53D6;&#x65F6;&#x8C03;&#x7528;compact,&#x51CF;&#x5C11;&#x5185;&#x5B58;&#x62F7;&#x8D1D;</span>
        readBuffer.compact();
    } <span class="hljs-keyword">else</span> {
        readBuffer.position(readBuffer.limit());
        readBuffer.limit(readBuffer.capacity());
    }
    continueRead();
}
</code></pre>
<p>&#x5199;&#x64CD;&#x4F5C;&#x7684;&#x56DE;&#x8C03;&#x4EE3;&#x7801;&#x5B9E;&#x73B0;&#x76F8;&#x5BF9;&#x7B80;&#x5355;&#x5F88;&#x591A;&#xFF0C;&#x4F46;&#x4E8B;&#x5B9E;&#x4E0A;&#x5374;&#x662F;&#x5904;&#x7406;&#x96BE;&#x5EA6;&#x6700;&#x5927;&#x7684;&#x3002;&#x56E0;&#x4E3A;&#x5199;&#x64CD;&#x4F5C;&#x662F;&#x4E2A;&#x4E3B;&#x52A8;&#x884C;&#x4E3A;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x53EF;&#x9884;&#x77E5;&#x7528;&#x6237;&#x5728;&#x4F55;&#x65F6;&#x4F1A;&#x5199;&#x5165;&#x6570;&#x636E;&#xFF0C;&#x800C;&#x5199;&#x7F13;&#x51B2;&#x533A;&#x4E2D;&#x79EF;&#x538B;&#x7684;&#x6570;&#x636E;&#x53C8;&#x8981;&#x4F9D;&#x9760;smart-socket&#x5C06;&#x5176;&#x8F93;&#x51FA;&#x5230;&#x7F51;&#x7EDC;&#x5BF9;&#x7AEF;&#xFF0C;&#x6545;&#x6B64;&#x5904;&#x5C31;&#x5B58;&#x5728;&#x5E76;&#x53D1;&#x7684;&#x60C5;&#x51B5;&#x3002;&#x5982;&#x679C;&#x51FA;&#x73B0;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x89E6;&#x53D1;write&#x64CD;&#x4F5C;&#x4F1A;&#x5BFC;&#x81F4;WritePendingException&#xFF0C;&#x5728;AioSession&#x4E2D;&#x4F7F;&#x7528;&#x4E86;&#x4FE1;&#x53F7;&#x91CF;Semaphore&#x5B8C;&#x6210;&#x4E86;&#x540C;&#x6B65;&#x63A7;&#x5236;&#xFF0C;&#x5B9E;&#x73B0;&#x4E86;&#x6570;&#x636E;&#x6709;&#x5E8F;&#x7684;&#x8F93;&#x51FA;&#x3002;smart-socket&#x7684;&#x5199;&#x56DE;&#x8C03;&#x4F1A;&#x6267;&#x884C;<code>writeToChannel</code>&#x65B9;&#x6CD5;&#xFF0C;&#x800C;&#x8FD9;&#x4FE1;&#x53F7;&#x91CF;&#x5728;&#x4E4B;&#x524D;&#x5DF2;&#x7ECF;&#x901A;&#x8FC7;&#x5176;&#x4ED6;&#x9014;&#x5F84;&#x88AB;&#x9501;&#x5B9A;&#xFF0C;&#x6B64;&#x5904;&#x6682;&#x4E14;&#x4E0D;&#x63D0;&#x3002;&#x56DE;&#x8C03;&#x7684;&#x5904;&#x7406;&#x903B;&#x8F91;&#x4E3A;&#xFF1A;</p>
<ol>
<li>&#x8BC6;&#x522B;writeBuffer&#x6216;&#x7F13;&#x51B2;&#x96C6;&#x5408;bufList&#x4E2D;&#x662F;&#x5426;&#x5B58;&#x5728;&#x5F85;&#x8F93;&#x51FA;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x82E5;&#x6709;&#x5219;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x5199;&#x64CD;&#x4F5C;<code>continueWrite()</code>&#x3002;</li>
<li>&#x5982;&#x679C;&#x6570;&#x636E;&#x5DF2;&#x7ECF;&#x8F93;&#x51FA;&#x5B8C;&#x6BD5;&#xFF0C;&#x5219;&#x91CA;&#x653E;&#x4FE1;&#x53F7;&#x91CF;&#x3002;</li>
<li>&#x5982;&#x679C;&#x5F53;&#x524D;AioSession&#x5904;&#x4E8E;&#x4E0D;&#x53EF;&#x7528;&#x72B6;&#x6001;&#xFF0C;&#x5219;&#x5173;&#x95ED;&#x5F53;&#x524D;&#x4F1A;&#x8BDD;&#x3002;</li>
<li>&#x7531;&#x4E8E;&#x5E76;&#x53D1;&#x7684;&#x56E0;&#x7D20;&#x53EF;&#x80FD;&#x5728;&#x91CA;&#x653E;&#x4FE1;&#x53F7;&#x91CF;&#x4E4B;&#x540E;&#x53C8;&#x6709;&#x6570;&#x636E;&#x88AB;&#x5199;&#x8FDB;&#x6765;&#xFF0C;&#x4E14;&#x4F1A;&#x8BDD;&#x4F9D;&#x65E7;&#x5904;&#x4E8E;&#x53EF;&#x7528;&#x72B6;&#x6001;&#xFF0C;&#x5219;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4F1A;&#x53BB;&#x7ADE;&#x4E89;&#x4FE1;&#x53F7;&#x91CF;&#x8D44;&#x6E90;&#xFF0C;&#x5E76;&#x5728;&#x6210;&#x529F;&#x83B7;&#x53D6;&#x5230;&#x4FE1;&#x53F7;&#x91CF;&#x540E;&#x6267;&#x884C;&#x6570;&#x636E;&#x8F93;&#x51FA;&#x3002;</li>
</ol>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">writeToChannel</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">if</span> (writeBuffer == <span class="hljs-keyword">null</span>) {
        writeBuffer = byteBuf.bufList.poll();
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!writeBuffer.buffer().hasRemaining()) {
        writeBuffer.clean();
        writeBuffer = byteBuf.bufList.poll();
    }
    <span class="hljs-keyword">if</span> (writeBuffer != <span class="hljs-keyword">null</span>) {
        continueWrite(writeBuffer);
        <span class="hljs-keyword">return</span>;
    }
    semaphore.release();
    <span class="hljs-comment">//&#x6B64;&#x65F6;&#x53EF;&#x80FD;&#x662F;Closing&#x6216;Closed&#x72B6;&#x6001;</span>
    <span class="hljs-keyword">if</span> (status != SESSION_STATUS_ENABLED) {
        close();
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!byteBuf.isClosed()) {
        <span class="hljs-comment">//&#x4E5F;&#x8BB8;&#x6B64;&#x65F6;&#x6709;&#x65B0;&#x7684;&#x6D88;&#x606F;&#x901A;&#x8FC7;write&#x65B9;&#x6CD5;&#x6DFB;&#x52A0;&#x5230;writeCacheQueue&#x4E2D;</span>
        byteBuf.flush();
    }
}
</code></pre>
<p>&#x82E5;&#x60F3;&#x4E86;&#x89E3;smart-socket&#x5B8C;&#x6574;&#x7684;&#x5199;&#x64CD;&#x4F5C;&#x5904;&#x7406;&#x903B;&#x8F91;&#xFF0C;&#x8BF7;&#x9605;&#x8BFB;&#x6E90;&#x7801;<code>WriteBuffer.java</code>&#x3002;</p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; smartboot.org 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2019-07-07 14:07:57
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../客户端/" class="navigation navigation-prev " aria-label="Previous page: 2.3 客户端">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../SUMMARY.html" class="navigation navigation-next " aria-label="Next page: 2.5 小结">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.4 会话AioSession","level":"1.2.3.4","depth":3,"next":{"title":"2.5 小结","level":"1.2.3.5","depth":3,"path":"smart-socket/chapter-2/SUMMARY.md","ref":"smart-socket/chapter-2/SUMMARY.md","articles":[]},"previous":{"title":"2.3 客户端","level":"1.2.3.3","depth":3,"path":"smart-socket/chapter-2/客户端/README.md","ref":"smart-socket/chapter-2/客户端/README.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","search-plus","expandable-chapters-interactive","tbfed-pagefooter","github","github-buttons","edit-link"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy smartboot.org 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/smartboot/smart-socket"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"expandable-chapters-interactive":{},"github-buttons":{"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]},"edit-link":{"label":"Edit This Page","base":"https://gitee.com/smartboot/book/edit/master"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"smartboot 中文指南","language":"en","gitbook":"*","description":"本文涵盖了smartboot下所有项目的技术手册"},"file":{"path":"smart-socket/chapter-2/AioSession/README.md","mtime":"2019-07-07T06:07:57.812Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2020-04-02T05:22:17.739Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>
    
        
        <script src="../../../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

